#include "sr.h"

#define READY 1
#define RUNNING 2

static int state;

void
initPowerUp(void) {
    state = READY;
}

void
powerUpFire(int hasEventFlags, int *craneKnownFlags, int *cranePresentFlags) {

    if(GET_BIT(hasEventFlags, POWERONBIT)) {
        SET_BIT_TRUE(*craneKnownFlags, TRIGGERBIT);
        SET_BIT_TRUE(*cranePresentFlags, TRIGGERBIT);
    }

    if(GET_BIT(*craneKnownFlags, TRIGGERBIT)) {
        SET_BIT_TRUE(*craneKnownFlags, TRIGGERBIT);
    }
}

void
powerUpPostfire(int hasEventFlags, int craneKnownFlags, int cranePresentFlags) {

    if(state == READY) {
        if(GET_BIT(hasEventFlags, POWERONBIT)) {
            state = RUNNING;
        }
    }
    else
    if(state == RUNNING) {
        if(GET_BIT(hasEventFlags, SHUTDOWNBIT)) {
            state = READY;
        }
        else
        if(GET_BIT(craneKnownFlags, EMSTOPMERGERBIT) &&
           GET_BIT(cranePresentFlags, EMSTOPMERGERBIT)) {
            state = READY;
        }
    }
}
